import Vue from 'vue'
const SUCCESS = 1000000
const API_ROOT = server.root + '/newmanage'

/**
 * 广告
 */
const getIndexAdListResource = Vue.resource(API_ROOT + '/index/ad/list')
const newIndexAdResource = Vue.resource(API_ROOT + '/index/ad/add')
const updateIndexAdResource = Vue.resource(API_ROOT + '/index/ad/update')
const deleteIndexAdResource = Vue.resource(API_ROOT + '/index/ad/delete')
const getMiniAdListResource = Vue.resource(API_ROOT + '/mini/ad/list')
const newMiniAdResource = Vue.resource(API_ROOT + '/mini/ad/add')
const updateMiniAdResource = Vue.resource(API_ROOT + '/mini/ad/update')
const deleteMiniAdResource = Vue.resource(API_ROOT + '/mini/ad/delete')
const getMiniBannberListResource = Vue.resource(API_ROOT + '/mini/banner/list')
const newMiniBannerResource = Vue.resource(API_ROOT + '/mini/banner/add')
const deleteMiniBannerResource = Vue.resource(API_ROOT + '/mini/banner/delete')
const updateMiniBannerResource = Vue.resource(API_ROOT + '/mini/banner/update')
const swapMiniBannerResource = Vue.resource(API_ROOT + '/mini/banner/swap')

const getAdListResoure = Vue.resource(API_ROOT + '/index/banner/list')
const newAdResource = Vue.resource(API_ROOT + '/index/banner/add')
const updateAdResoure = Vue.resource(API_ROOT + '/index/banner/update')
const deleteAdResource = Vue.resource(API_ROOT + '/index/banner/delete')
const swapAdResource = Vue.resource(API_ROOT + '/index/banner/swap')

const getOpenAdListResoure = Vue.resource(API_ROOT + '/trade/guide/list')
const newOpenAdResource = Vue.resource(API_ROOT + '/trade/guide/save')
const deleteOpenAdResource = Vue.resource(API_ROOT + '/trade/guide/delete')
const swapOpenAdResource = Vue.resource(API_ROOT + '/trade/guide/swap')

// 闪屏
const getScreensListResource = Vue.resource(API_ROOT + '/screen/list')
const addScreensResource = Vue.resource(API_ROOT + '/screen/save')
const deleteScreenResource = Vue.resource(API_ROOT + '/screen/delete')
const getScreenDetailResource = Vue.resource(API_ROOT + '/screen/detail')

export const getIndexAdList = ({dispatch}, {start = 0, limit = 10000}) => {
  return getIndexAdListResource.get({
    start: start,
    limit: limit
  })
    .then((res) => {
      dispatch('INDEX_AD_LIST_NEED_FRESH', 0)
      if (+res.data.code === SUCCESS) {
        return Promise.resolve(res.data.data)
      }
      return Promise.reject(res.data)
    })
}
export const newIndexAd = ({dispatch}, params) => {
  return newIndexAdResource.save(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        return dispatch('INDEX_AD_LIST_NEED_FRESH', 1)
      }
      return Promise.reject(res.data)
    })
}

export const updateIndexAd = ({dispatch}, params) => {
  return updateIndexAdResource.save(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        return dispatch('INDEX_AD_LIST_NEED_FRESH', 1)
      }
      return Promise.reject(res.data)
    })
}

export const deleteIndexAd = ({dispatch}, id) => {
  return deleteIndexAdResource.save({
    id: id
  })
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        return dispatch('INDEX_AD_LIST_NEED_FRESH', 1)
      }
      return Promise.reject(res.data)
    })
}

export const toogleIndexAdListFresh = ({dispatch}, freshFlag) => {
  dispatch('INDEX_AD_LIST_NEED_FRESH', freshFlag)
}

export const getMiniAdList = ({dispatch}, {start = 0, limit = 10000}) => {
  return getMiniAdListResource.get({
    start: start,
    limit: limit
  })
    .then((res) => {
      dispatch('MINI_AD_LIST_NEED_FRESH', 0)
      if (+res.data.code === SUCCESS) {
        return Promise.resolve(res.data.data)
      }
      return Promise.reject(res.data)
    })
}

export const newMiniAd = ({dispatch}, params) => {
  return newMiniAdResource.save(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        return dispatch('MINI_AD_LIST_NEED_FRESH', 1)
      }
      return Promise.reject(res.data)
    })
}

export const updateMiniAd = ({dispatch}, params) => {
  return updateMiniAdResource.save(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        return dispatch('MINI_AD_LIST_NEED_FRESH', 1)
      }
      return Promise.reject(res.data)
    })
}

export const deleteMiniAd = ({dispatch}, id) => {
  return deleteMiniAdResource.save({
    id: id
  })
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        return dispatch('MINI_AD_LIST_NEED_FRESH', 1)
      }
      return Promise.reject(res.data)
    })
}

export const getMiniBannberList = ({dispatch}, {start = 0, limit = 10000}) => {
  return getMiniBannberListResource.get({
    start: start,
    limit: limit
  })
    .then((res) => {
      dispatch('MINI_BANNER_LIST_NEED_FRESH', 0)
      if (+res.data.code === SUCCESS) {
        return Promise.resolve(res.data.data)
      }
      return Promise.reject(res.data)
    })
}

export const newMiniBanner = ({dispatch}, params) => {
  return newMiniBannerResource.save(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        return dispatch('MINI_BANNER_LIST_NEED_FRESH', 1)
      }
      return Promise.reject(res.data)
    })
}

export const deleteMiniBanner = ({dispatch}, id) => {
  return deleteMiniBannerResource.save({
    id: id
  })
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        return dispatch('MINI_BANNER_LIST_NEED_FRESH', 1)
      }
      return Promise.reject(res.data)
    })
}

export const updateMiniBanner = ({dispatch}, params) => {
  return updateMiniBannerResource.save(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        return dispatch('MINI_BANNER_LIST_NEED_FRESH', 1)
      }
      return Promise.reject(res.data)
    })
}

export const swapMiniBanner = ({dispatch}, fromId, toId) => {
  return swapMiniBannerResource.save({
    fromId: fromId,
    toId: toId
  })
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        return Promise.resolve()
      }
      return Promise.reject(res.data)
    })
}

export const getAdList = ({dispatch}, {start=0, limit=10000, position=4}) => {
  return getAdListResoure.get({
    start: start,
    limit: limit,
    position: position
  })
    .then((res) => {
      dispatch('UPDATE_OPEN_NAME', res.data.data)
      dispatch('GAO_LIST_NEED_FRESH', 0)
      if (+res.data.code === SUCCESS) {
        return Promise.resolve(res.data.data)
      }
      return Promise.reject(res.data)
    })
}

export const newAd = ({dispatch}, params) => {
  return newAdResource.save(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        return dispatch('GAO_LIST_NEED_FRESH', 1)
      }
      return Promise.reject(res.data)
    })
}

export const updateAd = ({dispatch}, params) => {
  return updateAdResoure.save(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        return dispatch('GAO_LIST_NEED_FRESH', 1)
      }
      return Promise.reject(res.data)
    })
}

export const deleteAd = ({dispatch}, id) => {
  return deleteAdResource.save({
    id: id
  })
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        return dispatch('GAO_LIST_NEED_FRESH', 1)
      }
      return Promise.reject(res.data)
    })
}

export const swapAd = ({dispatch}, params) => {
  return swapAdResource.save(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        return Promise.resolve()
      }
      return Promise.reject(res.data)
    })
}

/**
 * 开户引导页接口
 */

export const getOpenAdList = ({dispatch}, {start=0, limit=10000, position=4}) => {
  return getOpenAdListResoure.get({
    start: start,
    limit: limit,
    position: position
  })
    .then((res) => {
      dispatch('UPDATE_OPEN_NAME', res.data.data)
      dispatch('GAO_LIST_NEED_FRESH', 0)
      if (+res.data.code === SUCCESS) {
        return Promise.resolve(res.data.data)
      }
      return Promise.reject(res.data)
    })
}

export const newOpenAd = ({dispatch}, params) => {
  return newOpenAdResource.save(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        return dispatch('GAO_LIST_NEED_FRESH', 1)
      }
      return Promise.reject(res.data)
    })
}

export const deleteOpenAd = ({dispatch}, id) => {
  return deleteOpenAdResource.save({
    id: id
  })
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        return dispatch('GAO_LIST_NEED_FRESH', 1)
      }
      return Promise.reject(res.data)
    })
}

export const swapOpenAd = ({dispatch}, params) => {
  return swapOpenAdResource.save(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        return Promise.resolve()
      }
      return Promise.reject(res.data)
    })
}

export const getScreensList = ({dispatch}, params) => {
  return getScreensListResource.get(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        return Promise.resolve(res.data.data)
      }
      return Promise.reject(res.data)
    })
}

export const addScreens = ({dispatch}, params) => {
  return addScreensResource.save(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        return Promise.resolve()
      }
      return Promise.reject(res.data)
    })
}

const updateScreenResource = Vue.resource(API_ROOT + '/screen/update')
export const updateScreen = ({dispatch}, params) => {
  return updateScreenResource.save(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        return Promise.resolve(res.data)
      }
      return Promise.reject(res.data)
    })
}

export const deleteScreen = ({dispatch}, id) => {
  return deleteScreenResource.save({
    id: id
  })
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        return Promise.resolve()
      }
      return Promise.reject(res.data)
    })
}

export const getScreenDetail = ({dispatch}, id) => {
  return getScreenDetailResource.get({
    id: id
  })
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        return Promise.resolve(res.data.data)
      }
      return Promise.reject(res.data)
    })
}

const insertBetaAdResource = Vue.resource(API_ROOT + '/index/betaAd/insert')
export const insertBetaAd = ({dispatch}, params) => {
  return insertBetaAdResource.save(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        return dispatch('GAO_LIST_NEED_FRESH', 1)
      }
      return Promise.reject(res.data)
    })
}

const updateBetaAdResource = Vue.resource(API_ROOT + '/index/betaAd/update')
export const updateBetaAd = ({dispatch}, params) => {
  return updateBetaAdResource.save(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        return dispatch('GAO_LIST_NEED_FRESH', 1)
      }
      return Promise.reject(res.data)
    })
}

const getBetaAdListResource = Vue.resource(API_ROOT + '/index/betaAd/list')
export const getBetaAdList = ({dispatch}, params) => {
  return getBetaAdListResource.get(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        dispatch('GAO_LIST_NEED_FRESH', 0)
        return Promise.resolve(res.data.data)
      }
      return Promise.reject(res.data)
    })
}

const deleteBetaAdResource = Vue.resource(API_ROOT + '/index/betaAd/delete')
export const deleteBetaAd = ({dispatch}, params) => {
  return deleteBetaAdResource.save(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        return dispatch('GAO_LIST_NEED_FRESH', 1)
      }
      return Promise.reject(res.data)
    })
}
